package cocurrent;

import org.junit.Test;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {
    /**
     * CyclicBarrier 达到某个线程数之前，多个线程一起阻塞等待 ，直到达到某个数，这些线程才放行
     * @throws InterruptedException
     */
    @Test
    public void test1() throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(20, () -> {
            System.out.println("go~");
        });
        for (int i = 0; i < 100; i++) {
            Thread thread = new Thread(() -> {
                try {
                    cyclicBarrier.await();
                    System.out.println("done~");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
        Thread.sleep(10000);

    }



}
